<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('处方详情')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <style>
        .drug-item {
            border: solid thin #ddd;
            text-align: center;
            padding: 10px;
            margin: 5px 10px;
        }

        #scroll-pane-bottom {
            overflow-y: scroll;
            overflow-x: hidden;
        }

        .patient-info {
            margin: 5px 0px;
        }

        .hand {
            cursor: pointer;
        }

        .nostock {
            background-color: #f0e6e6;
        }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12">
                <form class="form-horizontal m" id="form-records-add" th:object="${tPrescriptionRecords}">
                    <input name="id" th:field="*{id}" type="hidden">
                    <div class="form-group scroll-pane-top">
                        <div class="row patient-info">
                            <div class="col-sm-6">
                                <label class="col-sm-3 control-label is-required">姓名：</label>
                                <div class="col-sm-3">
                                    <input name="name" th:field="*{name}" readonly
                                           class="form-control" type="text" required>
                                </div>

                                <label class="col-sm-3 control-label is-required">性别：</label>
                                <div class="col-sm-3">
                                    <select name="sex" id="sex" class="form-control" readonly
                                            th:with="type=${@dict.getType('sys_user_sex')}" onchange="change()">
                                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                                th:value="${dict.dictValue}" th:field="*{sex}"></option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <label class="col-sm-3 control-label is-required">年龄：</label>
                                <div class="col-sm-3">
                                    <input name="age" th:field="*{age}" id="age" readonly
                                           class="form-control" type="number" required>
                                </div>
                            </div>
                        </div>
                        <div class="row patient-info">
                            <label class="col-sm-2 control-label">电话：</label>
                            <div class="col-sm-3">
                                <input name="phone" th:field="*{phone}" readonly
                                       class="form-control" type="text">
                            </div>

                            <label class="col-sm-2 control-label">身份证号：</label>
                            <div class="col-sm-3">
                                <input name="idNumber" th:field="*{idNumber}" readonly
                                       class="form-control" type="text"
                                       onchange="idNumber_change()">
                            </div>
                        </div>
                        <div class="row patient-info">
                            <label class="col-sm-2 control-label">诊断：</label>
                            <div class="col-sm-10">
                                <textarea name="diagnosis" class="form-control" readonly>[[*{diagnosis}]]</textarea>
                            </div>
                        </div>
                        <div class="row patient-info" th:if="*{hasps==1}">
                            <label class="col-sm-3 control-label">皮试结果：</label>
                            <label class="col-sm-3 control-label">
                                <select class="form-control" id="psResult" onchange="psResult_change()"
                                        th:with="type=${@dict.getType('sys_ps_status')}">
                                    <option value="">请选择</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                            th:value="${dict.dictValue}" th:field="*{skinTest}"></option>
                                </select>
                            </label>
                        </div>
                    </div>
                    <div id="scroll-pane-bottom">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="col-sm-12" id="zsl-html"></div>
                                <div class="col-sm-12" id="fzsl-html"></div>
                                <div class="col-sm-12" id="qt-html"></div>
                                <div class="col-sm-12 select-table table-striped">
                                    <a name="cfxq"></a>
                                    <table id="bootstrap-table" data-use-row-attr-func="true"
                                           data-reorderable-rows="true"></table>
                                </div>
                            </div>
                        </div>
                        <!--                        <div class="row" style="margin-top: 10px">-->
                        <!--                            <div class="col-sm-12">-->
                        <!--                                <div class="col-sm-9"></div>-->
                        <!--                                <div id="zsje" class="col-sm-3" style="display: none">注射金额:</div>-->
                        <!--                            </div>-->
                        <!--                        </div>-->
                        <!--                        <div class="row">-->
                        <!--                            <div class="col-sm-12">-->
                        <!--                                <div class="col-sm-9"></div>-->
                        <!--                                <div id="ypje" class="col-sm-3" style="display: none">药品金额:</div>-->
                        <!--                            </div>-->
                        <!--                        </div>-->
                        <!--                        <div class="row">-->
                        <!--                            <div class="col-sm-12">-->
                        <!--                                <div class="col-sm-9"></div>-->
                        <!--                                <div id="zje" class="col-sm-3" style="display: none">总金额:</div>-->
                        <!--                            </div>-->
                        <!--                        </div>-->
                    </div>
                </form>
            </div>
            <div class="row">
                <div class="col-sm-8">
                    <div class="col-sm-2">操作人</div>
                    <div class="col-sm-2">医生</div>
                    <div class="col-sm-2">药房</div>
                    <div class="col-sm-2">护士</div>
                </div>
            </div>
            <div class="row" th:object="${tPrescriptionRecords}">
                <div class="col-sm-8">
                    <div class="col-sm-2"></div>
                    <div class="col-sm-2">[[*{doctorName}]]</div>
                    <div class="col-sm-2">[[*{pharmacyName}]]</div>
                    <div class="col-sm-2">[[*{nurseName}]]</div>
                </div>
            </div>
            <div class="row" style="text-align: center;margin: 20px">
                <div class="col-sm-12">
                    <button class="btn btn-primary" type="button" style="margin:5px 12px;" th:if="${canback}"
                            onclick="backtoDoctor_clicked()">
                        返回医生
                    </button>
                    <button th:if="${tPrescriptionRecords.hasps==0}" class="btn btn-primary" type="button"
                            style="margin:5px 12px;"
                            onclick="wanjie_clicked()">
                        已完成
                    </button>
                </div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: datetimepicker-js"/>
    <script th:src="@{/js/common.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "system/history"

        var mode = [[${mode}]];
        // 是否为详情页面
        var isDetail = mode == "detail";
        // 禁用的属性内容
        var readonlyStr = isDetail ? "readonly" : ""
        // 药品分类
        var drugTypes = [[${@dict.getType('sys_drug_type')}]];
        // 注射类的子类
        var business_zsl = [[${@dict.getType('business_zsl')}]];
        // 非注射类的子类
        var business_fzsl = [[${@dict.getType('business_fzsl')}]];
        // 其他类的子类
        var business_qt = [[${@dict.getType('business_qt')}]];

        $("#form-records-add").validate({
            focusCleanup: true
        });

        $("input[name='crtime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        // 输液费，固定值
        // var syf = 0;

        // 处方的药品信息
        var chufang_drugs = [[${tPrescriptionRecords.tPrescriptionDrugInfoList}]] || [];

        console.info(chufang_drugs)

        var options = {
            id: "bootstrap-table",
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            //data: chufang_drugs,
            onReorderRow: function (data) {
                //当拖拽结束后，data为整个表格的数据
                // 当sidePagination: "server"时，拖拽行后顺序错乱，需要重新调用加载数据方法
                chufang_drugs = data;
                refresh_tabledata();
                return false;
            },
            columns: [
                {
                    field: 'drugName',
                    align: 'center',
                    title: '名称',
                    visible: true,
                },
                {
                    field: 'drugType',
                    align: 'center',
                    title: '分类',
                    visible: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(drugTypes, value);
                    }
                },
                // {
                //     field: 'drugPrice',
                //     align: 'center',
                //     title: '零售价',
                //     visible: true,
                // },
                {
                    field: 'drugSpecs',
                    align: 'center',
                    title: '规格',
                    visible: true,
                },
                {
                    field: 'counts',
                    align: 'center',
                    title: '用药数量',
                    visible: true,
                },
                {
                    field: 'days',
                    align: 'center',
                    title: '用药天数',
                    visible: true,
                },
                // {
                //     field: 'amount',
                //     align: 'center',
                //     title: '合计金额',
                //     visible: true,
                // },
                {
                    field: 'remarks',
                    align: 'center',
                    title: '备注',
                    visible: true,
                },
            ],
        };

        $(function () {
            $.table.init(options);
            refresh_tabledata();
            // 获取数据字典内容
            // let syfInfo = [[${syf}]];
            // if (syfInfo == null) {
            //     $.modal.alertError('获取输液费失败')
            // } else {
            //     syf = syfInfo.retailPrice / 100;
            // }
            // updateZje()

            // $("input[evnt='changeDays']").keydown(function (event) {
            //     if (event.keyCode == 13) {
            //         setAllDays();
            //     }
            // });

            $("#scroll-pane-bottom").css("max-height", (window.screen.availHeight - 374) + "px");
        });


        function refresh_tabledata() {
            $("#bootstrap-table").bootstrapTable("load", chufang_drugs);
        }

        // 当前输液费
        // let tmpSyf = [[${tPrescriptionRecords.zsAmount}]];
        // 药品总金额
        // let drug_amount = [[${tPrescriptionRecords.drugAmount}]];
        // 处方总金额
        // let total_amount = [[${tPrescriptionRecords.totalAmount}]];

        // 更新总金额
        // function updateZje() {
        //     console.log(tmpSyf, "tmpSyf")
        //     if (chufang_drugs.find(a => a.drugType == "business_zsl") != undefined) {
        //         tmpSyf = syf;
        //         $("#zsje").css("display", "block").text("注射金额：" + tmpSyf);
        //         console.log(tmpSyf, "tmpSyf")
        //     } else {
        //         tmpSyf = 0;
        //         $("#zsje").css("display", "none");
        //         console.log(tmpSyf, "tmpSyf")
        //     }
        //     drug_amount = 0;
        //     total_amount = 0;
        //     chufang_drugs.forEach(a => drug_amount += a.amount);
        //     total_amount = tmpSyf + drug_amount;
        //
        //     $("#ypje").css("display", "block").text("药品金额：" + drug_amount);
        //     $("#zje").css("display", "block").text("总金额：" + total_amount);
        //     $("#zje2").css("display", "block").text("总金额：" + total_amount);
        // }

        function idNumber_change() {
            let value = event.target.value.trim();
            if (value.length != 18)
                return;

            let birthday = value.substr(6, 8)
            let year = Number(birthday.substr(0, 4))
            let month = Number(birthday.substr(4, 2))
            let day = Number(birthday.substr(6, 2))
            let birthdate = new Date(year, month - 1, day)
            let age = Math.floor((new Date() - birthdate) / (1 * 24 * 60 * 60 * 1000) / 365);
            $("#age").val(age)
        }


        // 点击返回医生
        function backtoDoctor_clicked() {
            var psresult = $("#psResult").val();
            if (psresult == "") {
                $.modal.alertWarning("请录入皮试结果");
                return;
            }
            post("/updatePrescription", {
                id: [[${tPrescriptionRecords.id}]],
                status: '0',
                skinTest: psresult,
                nurseid: [[${nurseid}]]
            }, function (res) {
                $.modal.alertSuccess(res.msg);
                $.modal.close();
            })
        }

        // 点击完结
        function wanjie_clicked() {
            post("/updatePrescription", {
                    id: [[${tPrescriptionRecords.id}]],
                    status: [[${tPrescriptionRecords.daysDetail-tPrescriptionRecords.over_days<=1?'3':'4'}]],
                    nurseid: [[${nurseid}]],
                },
                function (res) {
                    $.modal.alertSuccess(res.msg);
                    $.modal.close();
                }
            )
        }

        function post(url, data, success) {
            $.ajax({
                url: prefix + url,
                type: 'post',
                data: data,
                dataType: 'json',
                success: success,
            })
        }

        function postJson(url, data, success) {
            $.ajax({
                url: prefix + url,
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                success: success,
            })
        }

        function ps_change() {
            post("/updatePrescription", {id: [[${tPrescriptionRecords.id}]], skinTest: "阴"}, function (res) {
                $.modal.alertSuccess(res.msg);
            })
        }

        function psResult_change() {
            let res = $("#psResult").val();
            $.modal.alertWarning(`您选择的皮试结果为【${res}】`)
        }

    </script>
</body>
</html>